import { defineStore } from 'pinia'
import { ref } from 'vue'


interface historyData {
    name: string
}

export const useSearchHistoryStore = defineStore(
    'search',
    () => {
        const searchHistoryList = ref<historyData[]>([])

        const addSearchWord = (key :historyData) => {

            if(searchHistoryList.value.length > 0){
                const tmpCollection: historyData[] = searchHistoryList.value.filter(item=>item.name !== key.name).slice(0,9)
                tmpCollection.unshift(key)
                searchHistoryList.value = tmpCollection
            }
            else {
                searchHistoryList.value = [key]
            }
        }

        const delSearchWordByName = ( name: String) =>{
            if(searchHistoryList.value.length > 0){
                searchHistoryList.value = searchHistoryList.value.filter(item => item.name !== name)
            }
        }

        const clearSearchHistory = ()=>{
            searchHistoryList.value = []
        }

        return {
            addSearchWord,
            searchHistoryList,
            delSearchWordByName,
            clearSearchHistory
        }
    },
    {
        persist: true
    }
)
